package com.tssm.action.usercenter;

import java.io.File;
import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.tssm.cache.memcached.TsSessionUtil;
import com.tssm.core.util.MD5;
import com.tssm.core.util.Picfiles;
import com.tssm.pojo.SarcSupplier;
import com.tssm.service.SarcSupplierService;

@Controller("informationAction")
@Scope("prototype")
public class InformationAction extends ActionSupport {

	private static final long serialVersionUID = 8439962187744710863L;	
	@Resource
	private SarcSupplierService sarcsupplierService;
	//供应商
	private SarcSupplier ssup;
	private String userName;
	private String oldPassword;
	private String password;
	private String repassword;
	private String email;
	private String reemail;
	private String realName;
	private String userType;
	private String corpName;
	private String jgCode;
	private String mobile;
	private String zone;
	private String extTel;
	private String tel;
	private String fax;
	private String postCode;
	private String sex;
	private String province;
	private String city;
	private File business_cert;
	private String jgcode;
	private String business_certFileName;
	private String cargo;
	private String rand;
	private MD5 md5 = new MD5();
	private String note;
	private String message;
	private String oldpic;
	
	/**
	 * 密码修改
	 * @return
	 * @author：陶冶
	 * @date：2012-2-5
	 */
	public String newInfoPwd() {
		HttpServletRequest request = ServletActionContext.getRequest();
		ssup = TsSessionUtil.getTsUser(request);
		if(note == null) note = "";
		return "newInfoPwd";
	}
	/**
	 * 保存新密码
	 * @return
	 * @author：陶冶
	 * @date：2012-2-5
	 */
	public String saveInfoPwd() {
		HttpServletRequest request = ServletActionContext.getRequest();
		ssup = TsSessionUtil.getTsUser(request);
		if(ssup.getPassword().equals(md5.calcMD5(oldPassword))) {
			if(password.equals(repassword)) {
				ssup.setPassword(md5.calcMD5(password));
				sarcsupplierService.saveOrUpdate(ssup);
				note = "密码修改成功！";
			}else note = "对不起，重复新密码与新密码不一致，请重新输入！";
		}else note = "旧密码不正确，请重新输入！";
		return "saveInfoPwd";
	}
	/**
	 * 个人信息修改
	 * @return
	 * @author：陶冶
	 * @date：2012-2-5
	 */
	public String newInfo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		ssup = TsSessionUtil.getTsUser(request);
		ssup = sarcsupplierService.get(SarcSupplier.class, ssup.getSupplierid());
		
		System.out.println(URLEncoder.encode("调查表通过后才能录入报价单！", "UTF-8"));
		return "newInfos";
	}	
	/**
	 * 保存个人信息
	 * @return
	 * @author：陶冶
	 * @date：2012-2-5
	 */
	public String saveInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		ssup = TsSessionUtil.getTsUser(request);
		ssup.setSupplierName(userName);
		ssup.setName(realName);
//		ssup.setSupplierName(corpName);
		ssup.setSex(sex);
		ssup.setTel(tel);
		ssup.setFax(fax);
		ssup.setEmail(email);
		ssup.setProvince(province);
		ssup.setCity(city);
		ssup.setOrgCode(jgcode);
		ssup.setSuppliserCargo(cargo);
		String businessCert = "";
		if ( business_certFileName != null && !("").equals(business_certFileName)) {
			businessCert = Picfiles.getsrcpic(request, business_certFileName,
					business_cert, "/upload/cert/", 1000);
			ssup.setBusinessCert(businessCert);
		}
		if (businessCert != null && !("").equals(businessCert)) {
			Byte audit = 0;
			ssup.setAccountState(audit);
		}
		boolean flag=sarcsupplierService.saveOrUpdate(ssup);
		if (flag){
			if (businessCert != null && !("").equals(businessCert)) {
				if (!oldpic.equals(businessCert)) {
					oldpic = request.getSession().getServletContext()
							.getRealPath(oldpic);
					Picfiles.delFile(oldpic);
				}
			}
		}
		
		return "saveInfo";
	}
	/**
	 * 邮箱验证
	 * @return
	 * @author：陶冶
	 * @date：2012-2-5
	 */
	public String newInformationEmail() {
		HttpServletRequest request = ServletActionContext.getRequest();
		ssup = TsSessionUtil.getTsUser(request);
		return "newInformationEmail";
	}
	/**
	 * 邮箱验证
	 * @return
	 * @author：陶冶
	 * @date：2012-2-5
	 */
	public String saveInformationEmail() {
		HttpServletRequest request = ServletActionContext.getRequest();
		ssup = TsSessionUtil.getTsUser(request);
		return "saveInformationEmail";
	}
	
	public SarcSupplierService getSarcsupplierService() {
		return sarcsupplierService;
	}
	public void setSarcsupplierService(SarcSupplierService sarcsupplierService) {
		this.sarcsupplierService = sarcsupplierService;
	}
	public SarcSupplier getSsup() {
		return ssup;
	}
	public void setSsup(SarcSupplier ssup) {
		this.ssup = ssup;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getReemail() {
		return reemail;
	}
	public void setReemail(String reemail) {
		this.reemail = reemail;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public String getUserType() {
		return userType;
	}
	public void setUserType(String userType) {
		this.userType = userType;
	}
	public String getCorpName() {
		return corpName;
	}
	public void setCorpName(String corpName) {
		this.corpName = corpName;
	}
	public String getJgCode() {
		return jgCode;
	}
	public void setJgCode(String jgCode) {
		this.jgCode = jgCode;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getZone() {
		return zone;
	}
	public void setZone(String zone) {
		this.zone = zone;
	}
	public String getExtTel() {
		return extTel;
	}
	public void setExtTel(String extTel) {
		this.extTel = extTel;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getFax() {
		return fax;
	}
	public void setFax(String fax) {
		this.fax = fax;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public File getBusiness_cert() {
		return business_cert;
	}
	public void setBusiness_cert(File business_cert) {
		this.business_cert = business_cert;
	}
	public String getBusiness_certFileName() {
		return business_certFileName;
	}
	public void setBusiness_certFileName(String business_certFileName) {
		this.business_certFileName = business_certFileName;
	}
	public String getCargo() {
		return cargo;
	}
	public void setCargo(String cargo) {
		this.cargo = cargo;
	}
	public String getRand() {
		return rand;
	}
	public void setRand(String rand) {
		this.rand = rand;
	}
	public MD5 getMd5() {
		return md5;
	}
	public void setMd5(MD5 md5) {
		this.md5 = md5;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public String getPostCode() {
		return postCode;
	}
	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getJgcode() {
		return jgcode;
	}
	public void setJgcode(String jgcode) {
		this.jgcode = jgcode;
	}
	public String getOldpic() {
		return oldpic;
	}
	public void setOldpic(String oldpic) {
		this.oldpic = oldpic;
	}

}
